<?php

namespace Frieza\Modules\Photo\Extensions;

use Encore\Admin\Admin;

class FoldRow
{
    protected $id;
    private $isHidden;

    public function __construct($id, $isHidden)
    {
        $this->id = $id;
        $this->isHidden = $isHidden;
    }

    protected function script()
    {
        return <<<SCRIPT

$('.operate-fold').on('click', function () {
    var _this = $(this);
    let noteId = $(this).data('id');
    let type = 'fold';
    layer.confirm('确定要折叠这个回答？', {
        btn: ['与话题无关','内容违规'] //按钮
    }, function(index){
        let params = {note_id:noteId, type:type, extra_msg: 'not_related'}
        layer.close(index);
        $.post(url, params , function (e) {
            if(e.code == 1) {
                _this.addClass('hidden');
                _this.siblings('.operate-unfold').removeClass('hidden');  
     
                toastr.success(e.msg, null, {
                    timeOut : 1000
                });  
            } else {
                toastr.error(e.msg, null, {
                    timeOut : 1000
                });
            }
        });
    }, function(index){
        let params = {note_id:noteId, type:type, extra_msg: 'illegal'}
        layer.close(index);
        $.post(url, params , function (e) {
            if(e.code == 1) {
                _this.addClass('hidden');
                _this.siblings('.operate-unfold').removeClass('hidden');  
                toastr.success(e.msg, null, {
                    timeOut : 1000
                });  
            } else {
                toastr.error(e.msg, null, {
                    timeOut : 1000
                });
            }
        });
    });
});

SCRIPT;
    }

    public function render()
    {
        Admin::script($this->script());
        $hidden = $this->isHidden ? "hidden" : "";

        return "<a class='btn btn-xs btn-success fa fa-check operate-fold {$hidden}' data-id='{$this->id}'>折叠</a>";
    }

    public function __toString()
    {
        return $this->render();
    }
}
